package com.shop.loan.service.impl;

import com.shop.loan.mapper.BlacklistByMybatisMapper;
import com.shop.loan.mapper.UserByMybatisMapper;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.entity.BlacklistByMybatis;
import com.shop.loan.model.entity.UserByMybatis;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import com.shop.loan.rentaldata.RentalDataUtil;
import com.shop.loan.service.BlackListService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BlackListServiceImpl implements BlackListService {

    @Resource
    BlacklistByMybatisMapper blacklistByMybatisMapper;

    @Resource
    UserByMybatisMapper userByMybatisMapper;

    @Resource
    RentalDataUtil rentalDataUtil;

    @Resource
    RedisUtil redisUtil;

    @Override
    public ResResult blackPut() {
        Long lastId = null;
        Long lastIdByUser = null;
        List<String> blacklist = new ArrayList<>();

        Long dbLastId = null;
        String dbLastIdStr = redisUtil.get(RedisCache.putBlackLastId);
        if(StringUtils.hasText(dbLastIdStr)){
            dbLastId = Long.parseLong(dbLastIdStr);
        }

        Long dbLastIdByUser = null;
        String dbLastIdByUserStr = redisUtil.get(RedisCache.putBlackLastIdByUser);
        if(StringUtils.hasText(dbLastIdByUserStr)){
            dbLastIdByUser = Long.parseLong(dbLastIdByUserStr);
        }

        List<BlacklistByMybatis> blacklistByMybatisList = blacklistByMybatisMapper.selectByList(dbLastId);
        if (blacklistByMybatisList != null) {
            for (int i = 0; i < blacklistByMybatisList.size(); i++) {
                if (i == 0) {
                    lastId = blacklistByMybatisList.get(i).getId();
                }
                blacklist.add(blacklistByMybatisList.get(i).getContent());
            }
        }

        List<UserByMybatis> rentalUserByMybatisList = userByMybatisMapper.selectByBlack(dbLastIdByUser);
        if (rentalUserByMybatisList != null && rentalUserByMybatisList.size() > 0) {
            for (int i = 0; i < rentalUserByMybatisList.size(); i++) {
                if(i == 0){
                    lastIdByUser = rentalUserByMybatisList.get(i).getId();
                }
                if (StringUtils.hasLength(rentalUserByMybatisList.get(i).getMobile())) {
                    blacklist.add(rentalUserByMybatisList.get(i).getMobile());
                }
                if (StringUtils.hasLength(rentalUserByMybatisList.get(i).getCardNum())) {
                    blacklist.add(rentalUserByMybatisList.get(i).getCardNum());
                }
            }
        }
        rentalDataUtil.blackPut(blacklist);

        if (lastId != null) {
            redisUtil.set(RedisCache.putBlackLastId, lastId + "");
        }
        if (lastIdByUser != null) {
            redisUtil.set(RedisCache.putBlackLastIdByUser, lastIdByUser + "");
        }
        return new ResResult(Code.SUCCESS);
    }
}
